﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using DevComponents.DotNetBar;

namespace Quan_Ly_Dai_Ly
{
    public partial class TraCuuDaiLy : Office2007Form
    {
        public TraCuuDaiLy()
        {
            InitializeComponent();
        }

        private void TraCuuDaiLy_Load(object sender, EventArgs e)
        {   
            tb_Ten.Enabled = false;
            tb_DiaChi.Enabled = false;
            tb_SoDT.Enabled = false;
            tb_TienNo.Enabled = false;
            d_NgayTN.Enabled = false;
            cb_Loai.Enabled = false;
            cb_Quan.Enabled = false;
            d_NgayTN.Value = DateTime.Now;
            dg_DanhSach.Columns["DaiLy"].SortMode = DataGridViewColumnSortMode.Automatic;
            dg_DanhSach.Columns["Loai"].SortMode = DataGridViewColumnSortMode.Automatic;
            dg_DanhSach.Columns["Quan"].SortMode = DataGridViewColumnSortMode.Automatic;
            dg_DanhSach.Columns["TienNo"].SortMode = DataGridViewColumnSortMode.Automatic;
        }

        private void TraCuuDaiLy_Activated(object sender, EventArgs e)
        {  
            try
            {
                MainForm.Connect.Open();
                DataTable dt_Loai = new DataTable();
                DataTable dt_Quan = new DataTable();
                SqlDataAdapter da_Loai = new SqlDataAdapter("select * from LOAIDAILY order by CAST(TenLoaiDaiLy as nvarchar(200)) asc", MainForm.Connect);
                da_Loai.Fill(dt_Loai);
                cb_Loai.DataSource = dt_Loai;
                cb_Loai.DisplayMember = "TenLoaiDaiLy";
                cb_Loai.ValueMember = "MaLoaiDaiLy";
                SqlDataAdapter da_Quan = new SqlDataAdapter("select * from QUAN order by CAST(TenQuan as nvarchar(200)) asc", MainForm.Connect);
                da_Quan.Fill(dt_Quan);
                MainForm.Connect.Close();
                cb_Quan.DataSource = dt_Quan;
                cb_Quan.DisplayMember = "TenQuan";
                cb_Quan.ValueMember = "MaQuan";
                Lookup();
            }
            catch (SqlException sqlEx)
            {
                MainForm.Connect.Close();
                MessageBoxEx.Show("Lỗi kết nối cơ sở dữ liệu!\n" + sqlEx.Message, "Thông Báo");
            }
        }

        private void Lookup()
        {
            try
            {
                string TenDaiLy = "N'" + tb_Ten.Text + "'";
                string MaLoai = "N'" + cb_Loai.SelectedValue + "'";
                string DiaChi = "N'" + tb_DiaChi.Text + "'";
                string MaQuan = "N'" + cb_Quan.SelectedValue + "'";
                string SoDT = "'" + tb_SoDT.Text + "'";
                string Tien = "'" + tb_TienNo.Text + "'";
                string NgayTN = "'" + DateTime.ParseExact(d_NgayTN.Text, "dd/MM/yyyy", null) + "'";
                if (tb_Ten.Enabled == false)
                    TenDaiLy = "null";
                if (cb_Loai.Enabled == false)
                    MaLoai = "null";
                if (tb_DiaChi.Enabled == false)
                    DiaChi = "null";
                if (cb_Quan.Enabled == false)
                    MaQuan = "null";
                if (tb_SoDT.Enabled == false)
                    SoDT = "null";
                if (tb_TienNo.Enabled == false)
                    Tien = "null";
                if (d_NgayTN.Enabled == false)
                    NgayTN = "null";
                if (check_TatCa.Checked == false && tb_Ten.Enabled == false && cb_Loai.Enabled == false && tb_DiaChi.Enabled == false &&
                    cb_Quan.Enabled == false && tb_SoDT.Enabled == false && tb_TienNo.Enabled == false && d_NgayTN.Enabled == false)
                    TenDaiLy = MaLoai = DiaChi = MaQuan = SoDT = Tien = NgayTN = "''";
                if (MainForm.Connect.State == ConnectionState.Closed)
                    MainForm.Connect.Open();
                string query = "select * from f_TraCuuDaiLy(" + TenDaiLy + ","
                                                            + MaLoai + ","
                                                            + DiaChi + ","
                                                            + MaQuan + ","
                                                            + SoDT + ","
                                                            + NgayTN + ","
                                                            + Tien + ")";                                           
                SqlDataAdapter da = new SqlDataAdapter(query, MainForm.Connect);
                dt.Clear();
                da.Fill(dt);
                MainForm.Connect.Close();
                dg_DanhSach.DataSource = dt;
                STT.DataPropertyName = "STT";
                DaiLy.DataPropertyName = "DaiLy";
                Loai.DataPropertyName = "Loai";
                Quan.DataPropertyName = "Quan";
                TienNo.DataPropertyName = "TienNo";
                lb_KetQua.Text = "Số lượng đại lý được tìm thấy : " + dt.Rows.Count.ToString();
            }
            catch (SqlException sqlEx)
            {
                MainForm.Connect.Close();
                MessageBoxEx.Show("Lỗi kết nối cơ sở dữ liệu!\n" + sqlEx.Message, "Thông Báo");
            }
        }

        private void tb_Ten_TextChanged(object sender, EventArgs e)
        {
            Lookup();
        }

        private void check_Ten_CheckedChanged(object sender, EventArgs e)
        {
            if (check_Ten.Checked == true)
                check_TatCa.Checked = false;
            tb_Ten.Enabled = !tb_Ten.Enabled;
            Lookup();
        }

        private void tb_DiaChi_TextChanged(object sender, EventArgs e)
        {
            Lookup();
        }

        private void check_DiaChi_CheckedChanged(object sender, EventArgs e)
        {
            if (check_DiaChi.Checked == true)
                check_TatCa.Checked = false;
            tb_DiaChi.Enabled = !tb_DiaChi.Enabled;
            Lookup();
        }

        private void tb_SoDT_TextChanged(object sender, EventArgs e)
        {
            Lookup();
        }

        private void check_SoDT_CheckedChanged(object sender, EventArgs e)
        {
            if (check_SoDT.Checked == true)
                check_TatCa.Checked = false;
            tb_SoDT.Enabled = !tb_SoDT.Enabled;
            Lookup();
        }

        private void tb_SoDT_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }

        private void tb_TienNo_TextChanged(object sender, EventArgs e)
        {
            Lookup();
        }

        private void check_TienNo_CheckedChanged(object sender, EventArgs e)
        {
            if (check_TienNo.Checked == true)
                check_TatCa.Checked = false;
            tb_TienNo.Enabled = !tb_TienNo.Enabled;
            Lookup();
        }

        private void tb_TienNo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }

        private void cb_Loai_TextChanged(object sender, EventArgs e)
        {
            Lookup();
        }

        private void check_Loai_CheckedChanged(object sender, EventArgs e)
        {
            if (check_Loai.Checked == true)
                check_TatCa.Checked = false;
            cb_Loai.Enabled = !cb_Loai.Enabled;
            Lookup();
        }

        private void cb_Quan_TextChanged(object sender, EventArgs e)
        {
            Lookup();
        }

        private void check_Quan_CheckedChanged(object sender, EventArgs e)
        {
            if (check_Quan.Checked == true)
                check_TatCa.Checked = false;
            cb_Quan.Enabled = !cb_Quan.Enabled;
            Lookup();
        }

        private void d_NgayTN_TextChanged(object sender, EventArgs e)
        {
            Lookup();
        }

        private void check_NgayTN_CheckedChanged(object sender, EventArgs e)
        {
            if (check_NgayTN.Checked == true)
                check_TatCa.Checked = false;
            d_NgayTN.Enabled = !d_NgayTN.Enabled;
            Lookup();
        }

        private void check_TatCa_CheckedChanged(object sender, EventArgs e)
        {
            if (check_TatCa.Checked == true)
            {
                check_Ten.Checked = false;
                check_DiaChi.Checked = false;
                check_SoDT.Checked = false;
                check_TienNo.Checked = false;
                check_NgayTN.Checked = false;
                check_Loai.Checked = false;
                check_Quan.Checked = false;
            }
            Lookup();
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void dg_DanhSach_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1 || dg_DanhSach.Rows[e.RowIndex].Cells[0].Value.ToString().Length == 0)
                return;
            Form f = new ThongTinDaiLy(dt, e.RowIndex);
            f.MdiParent = this.MdiParent;
            f.Show();
        }

        DataTable dt = new DataTable();

    }
}
